<template>
  <div data-v-3b23e679="" class="gameTypeColumn">
    <div class="searchMaxBox">
      <div class="Carousel">
        <div class="Carousel_icon">
          <NuxtImg src="/images/layout/carousel_icon.png" />
        </div>
        <div class="carouselScrolling">
          <div class="carouselScrolling-container">
            <div class="searchBox">
              <div class="scroll-content" :style="marqueeInlineStyle">
                <!-- 第一份内容（用于测量与动画基准） -->
                <div class="marquee-copy" ref="marqueeCopyA">
                  <div
                    class="item"
                    v-for="item in noticeList"
                    :key="`a-${item.id}`"
                  >
                    <span>
                      {{ item.phone }}&nbsp; {{ item.label }}&nbsp;
                      <span> {{ item.money }}&nbsp;{{ item.unit }} </span>
                    </span>
                  </div>
                </div>
                <!-- 第二份内容（实现无缝循环） -->
                <div class="marquee-copy">
                  <div
                    class="item"
                    v-for="item in noticeList"
                    :key="`b-${item.id}`"
                  >
                    <span>
                      {{ item.phone }}&nbsp; {{ item.label }}&nbsp;
                      <span> {{ item.money }}&nbsp;{{ item.unit }} </span>
                    </span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div
        class="searchBox"
        v-click-move
        @click="emits('changeShowSearch', true)"
      >
        <NuxtImg
          class="search heat_btn2"
          src="/images/layout/icon-search.png"
        />
      </div>
    </div>
    <div class="columnb_box">
      <div class="columnb">
        <div
          class="link_box"
          v-for="item in gameTypeList"
          :key="item.id"
          @click="emits('changeGameType', item)"
        >
          <div
            class="link_box_active_box noCheck"
            :class="{ check: activeGameType.title === item.title }"
          >
            <div>
              <NuxtImg :src="item.img" />
            </div>
          </div>
          <span>{{ item.title }}</span>
        </div>
      </div>
    </div>
    <!---->
  </div>
</template>

<script setup lang="ts">
import { NuxtImg } from "#components";

defineProps({
  gameTypeList: {
    type: Array as any,
    default: () => [],
  },
  activeGameType: {
    type: Object,
    default: () => {},
  },
});

const emits = defineEmits(["changeGameType", "changeShowSearch"]);

const noticeList = ref<any[]>([]);
noticeList.value = new Array(10).fill({}).map((item, index) => {
  const id = index + 1;
  return {
    id,
    phone: `${id}*****${id}`,
    label: "acabou de retirar",
    money: `${id}52.99`,
    unit: "R$",
  };
});

// 处理通知公告无限滚动
const marqueeCopyA = ref<HTMLElement | null>(null);
let resizeObserver: ResizeObserver | null = null;
const PX_PER_SECOND = 60; // 每秒移动像素，调整此值即可统一速度
const marqueeDuration = ref(0);
const marqueeInlineStyle = computed(() => ({
  "--marquee-duration": `${marqueeDuration.value}s`,
}));
function initScrollNotice() {
  async function recalcMarqueeDuration() {
    await nextTick();
    const width = marqueeCopyA.value?.offsetWidth ?? 0;
    if (width > 0) {
      marqueeDuration.value = width / PX_PER_SECOND;
    }
  }

  onMounted(() => {
    recalcMarqueeDuration();
    if (marqueeCopyA.value && "ResizeObserver" in window) {
      resizeObserver = new ResizeObserver(() => recalcMarqueeDuration());
      resizeObserver.observe(marqueeCopyA.value);
    } else {
      window.addEventListener("resize", recalcMarqueeDuration);
    }
  });

  onBeforeUnmount(() => {
    if (resizeObserver && marqueeCopyA.value) {
      resizeObserver.unobserve(marqueeCopyA.value);
      resizeObserver.disconnect();
    } else {
      window.removeEventListener("resize", recalcMarqueeDuration);
    }
  });
}
if (import.meta.client) {
  initScrollNotice();
}
</script>

<style scoped lang="scss">
.gameTypeColumn {
  position: relative;
  margin-bottom: 0.4rem;
  margin-top: 0.3rem;

  .searchMaxBox {
    padding: 0 0.3rem;
    height: 1.2rem;
    color: var(--theme-neutral1);
    font-size: 0.32rem;
    box-sizing: border-box;
    margin-bottom: 0.25rem;
    display: grid;
    grid-template-columns: 1fr 1.2rem;
    gap: 0.2rem;

    .Carousel {
      width: 100%;
      height: 100%;
      padding-right: 0.1rem;
      display: grid;
      grid-template-columns: 0.84rem 1fr;
      align-items: center;
      background: #312734;
      border-radius: 0.2rem;
      padding-left: 0.2rem;
      border: 0.03rem solid var(--border-color);

      .Carousel_icon img {
        width: 1rem;
      }

      .carouselScrolling {
        margin: 0;
        padding: 0 0.2rem;
      }

      .searchBox {
        border: none;
        background: none;
      }
    }

    .searchBox {
      height: 1.2rem; // 只保留一个height属性
      border-radius: 0.2rem;
      display: flex;
      align-items: center;
      justify-content: center;
      background: #312734;
      border: 0.03rem solid var(--border-color);
      color: var(--theme-neutral2);
    }

    .search {
      height: 1rem;
    }
  }
}

.carouselScrolling {
  position: relative;
  height: 0.8rem;
  box-sizing: border-box;
  font-size: 0.32rem;
  overflow: hidden;
  margin: 0.2rem 0.8rem;
  color: var(--theme-neutral2);

  &:before {
    content: "";
    position: absolute;
    width: 100%;
    height: 100%;
    background: linear-gradient(
      90deg,
      var(--theme-ts4-gradient1),
      var(--theme-ts4-gradient2),
      var(--theme-ts4-gradient1)
    );
    opacity: 0.4;
  }

  .carouselScrolling-container {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    height: 100%;
    width: 100%;

    &:before,
    &:after {
      content: "";
      position: absolute;
      width: 100%;
      height: 0.03rem;
      background: linear-gradient(
        90deg,
        var(--theme-ts4-gradient1),
        var(--theme-ts4-gradient2),
        var(--theme-ts4-gradient1)
      );
      opacity: 0.8;
    }

    &:before {
      top: 0;
    }

    &:after {
      bottom: 0;
    }
  }
}

.searchBox {
  display: flex;
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
  position: relative;
}

// 匀速左移循环动画
@keyframes marquee-left {
  0% {
    left: 2.5rem;
    transform: translateX(0);
  }
  100% {
    left: 2.5rem;
    transform: translateX(-50%);
  }
}

.scroll-content {
  display: flex;
  flex-wrap: nowrap;
  width: max-content;
  will-change: transform;
  animation: marquee-left var(--marquee-duration, 0) linear infinite;
  position: relative;
  left: 2.5rem;
}

.scroll-content .marquee-copy {
  display: flex;
}

.item {
  display: flex;
  align-items: center;
  margin-right: 20px;
  font-size: 0.28rem;

  span {
    color: var(--theme-neutral1);

    span {
      color: var(--theme-dec4);
    }
  }
}

.heat_btn2 {
  transition: all 0.2s ease-in-out;
}

.columnb_box {
  text-align: center;

  // 统一设置所有子元素的过渡效果，只定义一次
  * {
    transition: all 0.2s ease-in-out;
  }

  .columnb {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    overflow-x: auto;
    gap: 0.2rem;
    padding: 0 0.3rem;
    white-space: nowrap;
    position: relative;
    width: 100%;
    flex-shrink: 0;
    scrollbar-width: none;
    -ms-overflow-style: none;
    padding-top: 0.3rem;
    margin-top: -0.3rem;

    // 隐藏Webkit浏览器的滚动条
    &::-webkit-scrollbar {
      display: none;
    }

    .link_box {
      position: relative;
      display: flex;
      flex-direction: column;
      text-decoration: none;

      .link_box_active_box {
        width: 1.2rem;
        height: 1.2rem;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        position: relative;
        background: url("/images/home/gameType/button_Def.png");
        background-size: 100% 100%;
        border-radius: 0.2rem;
        transition: transform 0.2s ease; // 合并transition属性

        div {
          display: flex;
          justify-content: center;
          align-items: center;
        }

        img {
          height: 0.9rem;
          position: relative;
          z-index: 2;
        }
      }

      span {
        color: var(--theme-neutral2);
        font-size: 0.3rem;
        position: relative;
        z-index: 2;
        margin-top: 0.12rem;
      }
    }
    @keyframes move-a {
      0% {
        transform: scale(1);
      }
      50% {
        transform: scale(1.2);
      }
      100% {
        transform: scale(1);
      }
    }
    .check {
      background: url("/images/home/gameType/button_Hight.png") !important;
      background-size: 100% 100% !important;
      animation: move-a 0.3s ease-out;
      + span {
        color: #fff !important;
      }
    }
  }
}
</style>
